
#!/bin/bash

export NDK_VERSION=24.0.8215888

# GITHUB WORKFLOWS
if [[ "$GITHUB_WORKFLOW" != "" ]]; then
    # Java 环境验证
    echo "Running workflow $GITHUB_WORKFLOW (event: $GITHUB_EVENT_NAME, action: $GITHUB_ACTION)"
    # Force Java to be Java 11 minimum, it defaults to 8 in GitHub runners for some platforms
    export JAVA_HOME=${JAVA_HOME_11_X64}
    java_version=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1)
    if [[ "$java_version" < 11 ]]; then
        echo "Android builds require Java 11, found version ${java_version} instead"
        exit 1
    fi

    # 安装NDK
    if [[ ! -d "${ANDROID_HOME}/ndk/$NDK_VERSION" ]]; then
        ${ANDROID_HOME}/tools/bin/sdkmanager "ndk;$NDK_VERSION" > /dev/null
    fi

    # Install ninja
    NINJA_VERSION=1.10.2
    wget -q https://github.com/ninja-build/ninja/releases/download/v$NINJA_VERSION/ninja-linux.zip
    unzip -q ninja-linux.zip
    export PATH="$PWD:$PATH"

    # Install CMake
    CMAKE_VERSION=3.23.0
    mkdir -p cmake
    cd cmake
    sudo wget -q https://github.com/Kitware/CMake/releases/download/v$CMAKE_VERSION/cmake-$CMAKE_VERSION-Linux-x86_64.sh
    sudo chmod +x ./cmake-$CMAKE_VERSION-Linux-x86_64.sh
    sudo ./cmake-$CMAKE_VERSION-Linux-x86_64.sh --skip-license > /dev/null
    sudo update-alternatives --install /usr/bin/cmake cmake $(pwd)/bin/cmake 1000 --force
    cd -


    export SDK_ROOT=$ANDROID_HOME
    export NDK_ROOT=${SDK_ROOT}/ndk/${NDK_VERSION}
fi


CURRENT_DIRECTORY=`pwd`
export LIB_ROOT_DIR=${CURRENT_DIRECTORY}/libs
export INSTALL_DIR=${LIB_ROOT_DIR}/android
export SOURCE_DIR=${LIB_ROOT_DIR}/sources_android
export BUILD_DIR=${LIB_ROOT_DIR}/build_android

echo ${CURRENT_DIRECTORY}

# android sdk root
if [ "$SDK_ROOT" == "" ]; then
    if [ "$ANDROID_HOME" != "" ]; then
        export SDK_ROOT=$ANDROID_HOME
    else
        echo "android sdk not exist"
        exit 1
    fi
fi

# NDK 环境变量
if [ "$NDK_ROOT" == "" ]; then
    export NDK_ROOT=${SDK_ROOT}/ndk/${NDK_VERSION}
fi

echo "NDK_ROOT=${NDK_ROOT}"

# TOOLCHAIN 
export TOOLCHAIN=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64

# SYSROOT
export SYSROOT=${TOOLCHAIN}/sysroot

# target host
export TARGET=aarch64-linux-android
export ANDROID_ABI=arm64-v8a
if [ "$1" = "arm64-android" ]; then
	TARGET=aarch64-linux-android
	ANDROID_ABI=arm64-v8a
elif [ "$1" = "arm-android" ]; then
	TARGET=armv7a-linux-androideabi
	ANDROID_ABI=armeabi-v7a
elif [ "$1" = "x64-android" ]; then
	TARGET=x86_64-linux-android
	ANDROID_ABI=x86_64
elif [ "$1" = "x86-android" ]; then
	TARGET=i686-linux-android
	ANDROID_ABI=x86
fi

# 更新安装目录
INSTALL_DIR=${INSTALL_DIR}/${ANDROID_ABI}
BUILD_DIR=${BUILD_DIR}/${ANDROID_ABI}

# ANDROID_NATIVE_API_LEVEL
export API_LEVEL=31

# autoconf make.
#export CC=$TOOLCHAIN/bin/$TARGET${API_LEVEL}-clang
#export CXX=$TOOLCHAIN/bin/$TARGET${API_LEVEL}-clang++
#export AR=$TOOLCHAIN/bin/llvm-ar
#export AS=$CC
#export LD=$TOOLCHAIN/bin/ld
#export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
#export STRIP=$TOOLCHAIN/bin/llvm-strip
PATH=$TOOLCHAIN/bin:$PATH
export CC=$TARGET${API_LEVEL}-clang
export CXX=$TARGET${API_LEVEL}-clang++
export AR=llvm-ar
export AS=$CC
export LD=ld
export RANLIB=llvm-ranlib
export STRIP=llvm-strip

# ./configure ; make ; make install 

# not autoconf make 
# make CC=$TOOLCHAIN/bin/$TARGET${API_LEVEL}-clang CXX=$TOOLCHAIN/bin/$TARGET${API_LEVEL}-clang++ AR=$TOOLCHAIN/bin/llvm-ar RANLIB=$TOOLCHAIN/bin/llvm-ranlib 

# cmake build
export CMAKE_ROOT=${SDK_ROOT}/cmake/3.20.0
export CMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake
export ANDROID_NDK=${NDK_ROOT}
export ANDROID_PLATFORM=android-${API_LEVEL}
# -DCMAKE_BUILD_TYPE=Debug
# -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
# -DANDROID_ABI=${ANDROID_ABI}
# -DANDROID_NDK=${ANDROID_NDK}
# -DANDROID_PLATFORM=${ANDROID_PLATFORM}
# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${INSTALL_DIR}
# -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${INSTALL_DIR}
# -DCMAKE_MAKE_PROGRAM=${CMAKE_ROOT}/bin/ninja
# -DCMAKE_SYSTEM_NAME=Android
# -DCMAKE_SYSTEM_VERSION=${API_LEVEL}
# -GNinja


rm -fr ${BUILD_DIR}
mkdir -p ${LIB_ROOT_DIR}
mkdir -p ${INSTALL_DIR}
mkdir -p ${SOURCE_DIR}
mkdir -p ${BUILD_DIR}

# ======================================install libs=====================================
LIB_VERSION=5.4.4
LIB_NAME=lua-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

# 安装目录不存在则进行安装
#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget http://www.lua.org/ftp/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}

	sed -i '9s/ gcc -std=gnu99/'" ${CC}"'/' ${LIB_SRC_DIR}/src/Makefile
	sed -i '10s/ / -fPIC /' ${LIB_SRC_DIR}/src/Makefile
	sed -i '14s/ ar/'" ${AR}"'/' ${LIB_SRC_DIR}/src/Makefile
	sed -i '15s/ ranlib/'" ${RANLIB}"'/' ${LIB_SRC_DIR}/src/Makefile

	make clean
    make 
    make local 
	mkdir -p ${LIB_INSTALL_DIR}

    mv ${LIB_SRC_DIR}/install/include ${LIB_INSTALL_DIR}
    mv ${LIB_SRC_DIR}/install/lib ${LIB_INSTALL_DIR}

    cd -
fi


# =================================Install LuaBridge===================================
LIB_VERSION=2.6
LIB_NAME=LuaBridge-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/vinniefalco/LuaBridge/archive/refs/tags/${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_INSTALL_DIR}/include
    cp -fr ${LIB_SRC_DIR}/Source/LuaBridge ${LIB_INSTALL_DIR}/include/LuaBridge
fi


# =================================Install SDL2===================================
LIB_VERSION=2.0.22
LIB_NAME=SDL2-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/libsdl-org/SDL/releases/download/release-${LIB_VERSION}/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

	cd ${LIB_SRC_DIR}
	./configure  --enable-static --enable-shared=no --enable-hidapi=no --with-pic --prefix=${LIB_INSTALL_DIR} --host=${TARGET} --with-sysroot=${SYSROOT}
	make 
	make install
    cd -
fi


# =================================Install libiconv===================================
LIB_VERSION=1.16
LIB_NAME=libiconv-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ftp.gnu.org/pub/gnu/libiconv/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	./configure  --enable-static --enable-shared=no --with-pic --enable-extra-encodings --without-libiconv-prefix --without-libintl-prefix --prefix=${LIB_INSTALL_DIR} --host=${TARGET} --with-sysroot=${SYSROOT}
    make
    make install
    cd -
fi


## =================================Install libuv===================================
#LIB_VERSION=1.44.1
#LIB_NAME=libuv-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://ghproxy.com/https://github.com/libuv/libuv/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi

    #cd ${LIB_SRC_DIR}
	#bash autogen.sh
	#./configure  --enable-static --enable-shared=no --with-pic --prefix=${LIB_INSTALL_DIR} --host=${TARGET} --with-sysroot=${SYSROOT}
    #make
    #make install
    #cd -
#fi


## =================================Install openssl===================================
#LIB_VERSION=3.0.2
#LIB_NAME=openssl-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
#OPENSSL_ROOT_DIR=${LIB_INSTALL_DIR}

##rm -fr ${LIB_INSTALL_DIR}
#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://www.openssl.org/source/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi
	#OPENSSL_TARGET="arm64-android"
	#OPENSSL_ANDROID_API=${API_LEVEL}

	#if [ "$TARGET" = "aarch64-linux-android" ]; then
		#OPENSSL_TARGET=android-arm64
	#elif [ "$TARGET" = "armv7a-linux-androideabi" ]; then
		#OPENSSL_TARGET=android-arm
	#elif [ "$TARGET" = "x64-android" ]; then
		#OPENSSL_TARGET=android-x86_64
	#elif [ "$TARGET" = "x86-android" ]; then
		#OPENSSL_TARGET=android-x86
	#fi
	#ANDROID_NDK_ROOT=${NDK_ROOT}

    #cd ${LIB_SRC_DIR}
	##make clean
	#./Configure ${OPENSSL_TARGET} -D__ANDROID_API__=${OPENSSL_ANDROID_API} --prefix=${LIB_INSTALL_DIR}
    #make
    #make install_dev
    #cd -
#fi


## =================================Install curl===================================
#LIB_VERSION=7.82.0
#LIB_NAME=curl-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
#CURL_ROOT_DIR=${LIB_INSTALL_DIR}

#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://curl.se/download/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi

    #cd ${LIB_SRC_DIR}
	##make clean
    #./configure --enable-static --enable-shared=no --with-pic --host=${TARGET} --prefix=${LIB_INSTALL_DIR} --with-openssl=${OPENSSL_ROOT_DIR}
    #make
    #make install
    #cd -
#fi


# =================================Install tinyxml2===================================
LIB_VERSION=9.0.0
LIB_NAME=tinyxml2-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
		    wget https://ghproxy.com/https://github.com/leethomason/tinyxml2/archive/refs/tags/${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_BUILD_DIR}
    cd ${LIB_BUILD_DIR}
	cmake -S ${LIB_SRC_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} -DANDROID_ABI=${ANDROID_ABI} -DANDROID_PLATFORM=${ANDROID_PLATFORM}
    make
    make install 
    cd -
fi


# =================================Install spdlog===================================
LIB_VERSION=1.10.0
LIB_NAME=spdlog-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/gabime/spdlog/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_BUILD_DIR}
    cd ${LIB_BUILD_DIR}
	cmake -S ${LIB_SRC_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} -DANDROID_ABI=${ANDROID_ABI} -DANDROID_PLATFORM=${ANDROID_PLATFORM}
    make
    make install 
    cd -
fi


# =================================Install CRYPTOPP===================================
#LIB_VERSION=8_6_0
#LIB_NAME=CRYPTOPP_${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

##rm -fr ${LIB_INSTALL_DIR}
#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
			#mv ${SOURCE_DIR}/cryptopp-${LIB_NAME} ${LIB_SRC_DIR}
        #else
			#wget https://ghproxy.com/https://github.com/weidai11/cryptopp/archive/refs/tags/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
			#mv ${SOURCE_DIR}/cryptopp-${LIB_NAME} ${LIB_SRC_DIR}
        #fi
    #fi

	#SETENV_ANDROID_SH=${LIB_SRC_DIR}/TestScripts/setenv-android.sh
	#sed -i 's/aarch64-linux-android-ld/ld/' ${SETENV_ANDROID_SH}
	#sed -i 's/aarch64-linux-android-as/llvm-as/' ${SETENV_ANDROID_SH}
	#sed -i 's/aarch64-linux-android-ar/llvm-ar/' ${SETENV_ANDROID_SH}
	#sed -i 's/aarch64-linux-android-ranlib/llvm-ranlib/' ${SETENV_ANDROID_SH}
	#sed -i 's/aarch64-linux-android-strip/llvm-strip/' ${SETENV_ANDROID_SH}
	#sed -i 's/aarch64-linux-android-objdump/llvm-objdump/' ${SETENV_ANDROID_SH}

	#sed -i 's/arm-linux-android-ld/llvm-ld/' ${SETENV_ANDROID_SH}
	#sed -i 's/arm-linux-android-as/llvm-as/' ${SETENV_ANDROID_SH}
	#sed -i 's/arm-linux-android-ar/llvm-ar/' ${SETENV_ANDROID_SH}
	#sed -i 's/arm-linux-android-ranlib/llvm-ranlib/' ${SETENV_ANDROID_SH}
	#sed -i 's/arm-linux-android-strip/llvm-strip/' ${SETENV_ANDROID_SH}
	#sed -i 's/arm-linux-android-objdump/llvm-objdump/' ${SETENV_ANDROID_SH}

	#cd ${LIB_SRC_DIR}

	#export IS_ANDROID=1
	#export ANDROID_NDK_ROOT=${NDK_ROOT}
	#export ANDROID_SDK_ROOT=${SDK_ROOT}
	#${SETENV_ANDROID_SH} ${API_LEVEL} ${ANDROID_ABI}

	##make clean
	#make install PREFIX=${LIB_INSTALL_DIR} -f GNUmakefile-cross

	#mkdir -p ${LIB_INSTALL_DIR}/lib 
	#cp libcryptopp.a ${LIB_INSTALL_DIR}/lib
    #cd -
#fi


# =================================Install libpng===================================
LIB_VERSION=1.6.35
LIB_NAME=libpng-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
LIBPNG_ROOT_DIR=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/glennrp/libpng/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	#make clean
	./configure  --enable-static --enable-shared=no --with-pic --host=${TARGET} --prefix ${LIB_INSTALL_DIR} 
    make 
    make install
    cd -
fi


# =================================Install pixman===================================
LIB_VERSION=0.40.0
LIB_NAME=pixman-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
PIXMAN_ROOT_DIR=${LIB_INSTALL_DIR}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://www.cairographics.org/releases/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	#make clean
    ./configure --enable-static --enable-shared=no --with-pic --host=${TARGET} --prefix=${LIB_INSTALL_DIR} PNG_CFLAGS="-I${LIBPNG_ROOT_DIR}/include/libpng16" PNG_LIBS="-L${LIBPNG_ROOT_DIR}/lib -lpng16" 
    make 
    make install
    cd -
fi


# =================================Install zlib===================================
LIB_VERSION=1.2.12
LIB_NAME=zlib-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
ZLIB_ROOT_DIR=${LIB_INSTALL_DIR}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget http://zlib.net/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	#make clean
    ./configure  --static --prefix=${LIB_INSTALL_DIR}
    make 
    make install
    cd -
fi


# =================================Install bzip2===================================
#LIB_VERSION=1.0.8
#LIB_NAME=bzip2-${LIB_VERSION}
#LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
#LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
#LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
#LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
#BZIP2_ROOT_DIR=${LIB_INSTALL_DIR}

#if [ ! -d ${LIB_INSTALL_DIR} ]; then
    #if [ ! -d ${LIB_SRC_DIR} ]; then
        #if [ -f ${LIB_SRC_TAR_GZ} ]; then
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #else
            #wget https://sourceware.org/pub/bzip2/bzip2-${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            #tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        #fi
    #fi

	#sed -i '18s/gcc/'"${CC}"'/' ${LIB_SRC_DIR}/Makefile
	#sed -i '24s/=/=-fPIC /' ${LIB_SRC_DIR}/Makefile
	#sed -i '19s/ar/'"${AR}"'/' ${LIB_SRC_DIR}/Makefile
	#sed -i '20s/ranlib/'"${RANLIB}"'/' ${LIB_SRC_DIR}/Makefile
    #cd ${LIB_SRC_DIR}
    #make 
    #make install PREFIX=${LIB_INSTALL_DIR}
    #cd -
#fi

# =================================Install freetype===================================
LIB_VERSION=2.12.0
LIB_NAME=freetype-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
FREETYPE_ROOT_DIR=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://download.savannah.gnu.org/releases/freetype/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	#make clean
    ./configure --enable-static --enable-shared=no \
		--with-bzip2=no \
		--with-pic \
		--host=${TARGET} \
		--target=${TARGET} \
		--prefix=${LIB_INSTALL_DIR} \
		LIBPNG_CFLAGS="-I${LIBPNG_ROOT_DIR}/include/libpng16" \
		LIBPNG_LIBS="-L${LIBPNG_ROOT_DIR}/lib -lpng16" \
		ZLIB_CFLAGS="-I${ZLIB_ROOT_DIR}/include" \
		ZLIB_LIBS="-L${ZLIB_ROOT_DIR}/lib -lz" 
	make 
	make install
	cd -
fi


# =================================Install cairo===================================
LIB_VERSION=1.16.0
LIB_NAME=cairo-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://www.cairographics.org/releases/${LIB_NAME}.tar.xz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	#make clean
    ./configure --enable-static \
        --enable-shared=no \
        --enable-xlib-xcb=no \
        --enable-xcb=no \
        --enable-xcb-shm=no \
        --enable-xlib=no \
        --enable-xlib-xrender=no \
        --enable-fc=no \
		--with-pic \
		--host=${TARGET} \
        --prefix=${LIB_INSTALL_DIR} \
        PKG_CONFIG_PATH="" \
		png_REQUIRES="yes" \
        png_CFLAGS="-I${LIBPNG_ROOT_DIR}/include/libpng16" \
        png_LIBS="-L${LIBPNG_ROOT_DIR}/lib -lpng16" \
        FREETYPE_CFLAGS="-I${FREETYPE_ROOT_DIR}/include/freetype2" \
        FREETYPE_LIBS="-L${FREETYPE_ROOT_DIR}/lib -lfreetype" \
        pixman_CFLAGS="-I${PIXMAN_ROOT_DIR}/include/pixman-1" \
        pixman_LIBS="-L${PIXMAN_ROOT_DIR}/lib -lpixman-1" 
	cd src
	make 
	make install
	cd -
fi


# ================================= external ===============================
bash `dirname $0`/build_external.sh
